﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

namespace MOWebApplication
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {           
            
            if (hidMouseX.Text != string.Empty && hidMouseY.Text != string.Empty)
            {
                int x = int.Parse(hidMouseX.Text);

                int y = int.Parse(hidMouseY.Text);

                //outputText.Text = String.Format("{0},{1}", x, y);
            }

            string usrName = "alex";
            Guid imgGuid;
            if (!IsPostBack)
            {

                string url;
                imgGuid = Guid.NewGuid();
                url = "http://localhost/scripts/esrimap.dll?name=MOclient&username=" + usrName + "&cmd=map"
                    + "&imgguid=" + imgGuid.ToString();

                TryGetWebLinkResponse(url);

                imgProduct.ImageUrl = "~/ImageHandler/ImageHandler.ashx?imgName="
                + imgGuid
                + "&usrName=" + usrName;
            }
            else
            {
                if (Session["imgGuid"]!=null)
                {
                    imgGuid = (Guid)Session["imgGuid"]  ;
                    imgProduct.ImageUrl = "~/ImageHandler/ImageHandler.ashx?imgName="
                                         + imgGuid
                                         + "&usrName=" + usrName;
                }
            }
           
        } 

        protected void hidMouse_TextChanged(object sender, EventArgs e)
        {
            int x = Int32.Parse(hidMouseX.Text);

            int y = Int32.Parse(hidMouseY.Text);
        }

        [System.Web.Services.WebMethod]
        public static void Dummy(int x, int y, string selectedTool
            //, Image imgProduct
            )
        {
            string tool = selectedTool;           

            if (selectedTool != String.Empty)
            {
                string url;
                string usrName = "alex";

                Guid imgGuid = Guid.NewGuid();
                url = "http://localhost/scripts/esrimap.dll?name=MOclient&username=" + usrName
                    + "&imgguid=" + imgGuid.ToString();

                switch (selectedTool)
                {
                    case "ZoomIn":
                        url += "&cmd=zoomin";

                        //temporally unused, but will be  calculated  somehow  later.                        
                        url += "&left=694";
                        url += "&top=888000";
                        url += "&right=1329000";
                        url += "&bottom=6000";
                        break;

                    case "ZoomOut":
                        url += "&cmd=zoomout";

                        //temporally unused, but will be  calculated  somehow  later
                        url += "&left=694";
                        url += "&top=888000";
                        url += "&right=1329000";
                        url += "&bottom=6000";
                        break;
                }

                url += "&click.x=" + x;
                url += "&click.y=" + y;

               
                TryGetWebLinkResponse(url);
                HttpContext.Current.Session["imgGuid"] = imgGuid;
              
            }    
            
        }


        private static void TryGetWebLinkResponse(string url)
        {
            try
            {
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

                    HttpWebResponse respons;

                    respons = (HttpWebResponse)request.GetResponse();

                    StreamReader reader = new StreamReader(respons.GetResponseStream());
                    string contents = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
